const client = require('scp2')
const ora = require('ora')
const chalk = require('chalk')
const spinner = ora('正在打包并发布到服务器...')
const { spawn } = require('child_process')

const config = {
  platformName: '尖米粒',
  host: '47.97.73.49',
  port: 22,
  username: 'root',
  password: 'szjmlkj@163',
  path: '/www/wwwroot/adminjml.smallrice.com.cn/'
}

spinner.start()

const pip = spawn('npm', ['run', 'build'])

pip.stdout.on('data', data => {
  if (data) console.log(`stdout: ${data}`)
})

pip.stderr.on('data', data => {
  if (data) console.log(`stderr: ${data}`)
})

pip.stdout.on('close', code => {
  client.scp(`dist`, config, err => {
    if (!err) {
      spinner.stop()
      spinner.stop()
      console.log(chalk.green('项目发布成功.\n'))
    } else {
      console.log(err)
      console.log(chalk.red('项目发布失败.\n'))
    }
  })
})
